package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.vo.UserVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
@RestController
@ResponseBody
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Operation(summary ="注册功能")
    @ApiOperationSupport(order = 20)
    @PostMapping("reg")
    public JsonResult reg(@RequestBody @Validated UserRegParam userRegParam){
        log.debug("开始处理【用户注册】的请求，参数：{}", userRegParam);
        String username = userRegParam.getUsername();
        Integer count = userMapper.selectUserByUsername(username);
        if(count !=null){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("user ="+ user);
        int i = userMapper.insertUser(user);
        if(i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @Operation(summary = "登录功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession session, HttpServletResponse response) throws IOException {
        log.debug("userloginParam:"+userLoginParam);
//        response.sendRedirect("/login.html");
        LocalDateTime now =LocalDateTime.now();
        int hour =now.getHour();
        if (hour<10||hour>22){
            response.sendRedirect("/error.html");
//            throw new RuntimeException("请在06:00-23:00 之间进行访问");
        }
        UserVo userVo = userMapper.selectUserByUsernameAndPassword(userLoginParam);
        if (userVo ==null){
            return JsonResult.ok(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user", userVo);

        return JsonResult.ok(userVo);

    }
    @GetMapping("currentUser")
    @Operation(summary = "获取当前用户")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(HttpSession session){
        //从session会话中获取用户标识
        UserVo userVo = (UserVo) session.getAttribute("user");
        /*
            服务端返回: response: {"state":2000,"message":"操作成功", "data": userVO}
            前端工程师:
            response.data:{"state":2000,"message":"操作成功", "data": userVO}
            response.data.data: userVO
            if (response.data.data == null){
                router.push("/login"); //跳转到登录页面
            }else{
                //从userVO中获取用户名
                router.push("/index.html"); //以登录的状态跳转到首页
            }
         */
        return JsonResult.ok(userVo);
    }
    @GetMapping("logout")
    @Operation(summary = "退出登录")
    @ApiOperationSupport(order = 40)
    public void logout(HttpSession session){
        session.removeAttribute("user");
        session.invalidate();
        log.debug("用户退出登录");

    }



}
